<?php

class StockAction extends Action {

	private $listStockStatement;
	private $getProductNameStatement;
	private $insertStatement;
	private $listShoppingListItemsStatement;
	
	function __construct(){
		parent::__construct();
		$this->listStockStatement = parent::prepareStatement(  "select pr.product_id, concat(pr.name , ' [ ' , pa.name, ' ( ', pa.quantity,  m.name, ' )' ,' ]') product_name
																from products pr, packageunits pa, metricsystemunits m
																where pr.package_unit_id = pa.package_unit_id
																and pa.metric_system_unit_id = m.metric_system_unit_id");
		$this->getProductNameStatement = parent::prepareStatement( "select concat(pr.name , ' [ ' , pa.name, ' ( ', pa.quantity,  m.name, ' )' ,' ]') product_name
																	from products pr, packageunits pa, metricsystemunits m
																	where pr.package_unit_id = pa.package_unit_id
																	and pa.metric_system_unit_id = m.metric_system_unit_id
																	and pr.product_id = ?");
		$this->insertStatement = parent::prepareStatement("insert into stocks (product_id, amount_available, amount_desired) values(?,?,?)");
		$this->listShoppingListItemsStatement = parent::prepareStatement(  "select concat(pr.name , ' [ ' , pa.name, ' ( ', pa.quantity,  m.name, ' )' ,' ]') product_name,
																			IFNULL((s.amount_desired - s.amount_available),0) amount
																			from stocks s, products pr, packageunits pa, metricsystemunits m
																			where s.product_id = pr.product_id
																			and pr.package_unit_id = pa.package_unit_id
																			and pa.metric_system_unit_id = m.metric_system_unit_id
																			and on_shoplist = true" );
	}
	
	function listProducts(){
		$this->listStockStatement->execute();
		$this->listStockStatement->bind_result($product_id, $product_name);

		$resultSet = array();
		while ($this->listStockStatement->fetch()) {
			array_push($resultSet, array($product_id, $product_name));
		}
		return $resultSet;
	}
	
	function getProductName($productId){
		$rc = $this->getProductNameStatement->bind_param("i", $productId);
		if ( false===$rc ) {
            printf('bind_param failed: %s', htmlspecialchars($this->getProductNameStatement->error));
			die;
        }
		
		$this->getProductNameStatement->execute();
		$this->getProductNameStatement->bind_result($product_name);

		$resultSet = array();
		while ($this->getProductNameStatement->fetch()) {
			array_push($resultSet, array($product_name));
		}
		if(sizeof($resultSet) > 1){
			throw new Exception("Multiple values found for product id (" . $productId . ")");
		}
		return $resultSet[0][0];
	}
	
	function insert($product_id, $amount_available, $amount_desired){
		if(!$product_id){
			throw new Exception('product_id cannot be null.');
		}
		if($amount_available == null){
			throw new Exception('amount_available cannot be null.');
		}
		if($amount_desired == null){
			throw new Exception('amount_desired cannot be null.');
		}
		
		$rc = $this->insertStatement->bind_param("iii", $product_id, $amount_available, $amount_desired);
		if ( false===$rc ) {
            printf('bind_param failed: %s', htmlspecialchars($this->insertStatement->error));
			die;
        }
		
		$rc = $this->insertStatement->execute();
        if ( false===$rc ) {
                printf('execute failed: %s', htmlspecialchars($this->insertStatement->error));
                die;
        }
	}
	
	function getShoppingList(){
		$this->listShoppingListItemsStatement->execute();
		$this->listShoppingListItemsStatement->bind_result($product_name, $amount);

		$resultSet = array();
		while ($this->listShoppingListItemsStatement->fetch()) {
			array_push($resultSet, array($product_name, $amount));
		}
		return $resultSet;
	}
}